<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends AuthController {
	/**
	 * 个人资料
	 */
    public function index(){
		$this->display();
    }
    /**
     * 收货地址列表
     */
    public function address(){
    	$store = session("store");
    	$where['uid'] = $store['id'];
    	$list = M("Address")->where($where)->select();
    	$count = count($list);
    	for($i = 0;$i<$count;$i++){
    		$item = $list[$i];
    		if($item['id'] == $store['address_id']){
    			array_splice($list, $i, 1);
    			array_splice($list, 0,0,array($item));
    		}
    	}
    	$areas = M('Area')->order(array('id' => 'asc'))->select();
    	$this->assign('areas',$areas);
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 添加收货地址
     */
    public function addAdr(){
    	$data = $_POST;
    	$store = session("store");
    	$data['uid'] =  $store['id'];
    	$address = D("Address");
    	$result = $address->add($data);
    	if($result){
    		$this->success("添加成功",'address');
    	}else{
    		$this->error("添加失败");
    	}
    }
    /**
     * 修改收货地址
     */
    public function changeAdd(){
    	if(IS_POST){
    		$store = session('store');
    		$data['address_id'] = $_POST['id'];
    		$where['id'] = $store['id'];
    		$user = D("User");
    		$result = $user->where($where)->save($data);
    		if($result){
    			$store['address_id'] = $_POST['id'];
    			session('store',$store);
    		}
    		$this->ajaxReturn($result);
    	}
    }
    /**
     * 删除收货地址
     */
    public function deleteAdd(){
    	if(IS_POST){
    		$where['id'] = $_POST["id"];
    		$result = M("Address")->where($where)->delete();
    		$this->ajaxReturn($result);
    	}
    }
    /**
     * 修改密码
     */
    public function pwd(){
    	if(IS_POST){
    		$oldpwd = $_POST['oldpwd'];
    		$newpwd = $_POST['newpwd'];
    		$repwd  = $_POST['repwd'];
    		if($repwd == $newpwd){
    			$store = session("store");
    			$where['id'] = $store['id'];
    			$where['password'] = md5($oldpwd);
    			$result = M("User")->where($where)->find();
    			if($result){
    				$data['password'] = md5($newpwd);
    				$result = M("User")->where($where)->save($data);
    				if($result){
    					$this->success("修改成功");
    				}else{
    					$this->error("修改失败！");
    				}
    			}else{
	    			$this->error("原密码错误！");
    			}
    		}else{
    			$this->error("两次密码输入不一致！");
    		}
    	}else{
    		$this->display();
    	}
    }
    /**
     * 订单列表
     */
    public function order(){
    	$order = D("Order");
    	$pageSize = 5;
    	$store = session('store');
    	$where['uid'] = $store['id'];
    	$list 		= $order->relation(true)->where($where)->page($_GET['p'],$pageSize)->order('cdate desc,ctime desc')->select();
    	$count      = $order->where($where)->count();// 查询满足要求的总记录数
    	$Page       = new \Think\Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
    	$Page->setConfig("next","下一页");
    	$Page->setConfig("prev","上一页");
    	$show       = $Page->show();// 分页显示输出
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign('totalPages',$Page->totalPages);// 总页数
    	$this->assign('list',$list);
    	$this->display();
    }
}